const axios = require('axios');

(async () => {
  console.log('🧪 测试新的勋章API...');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 测试修改后的勋章API
    console.log('\n🏆 测试修改后的勋章API...');
    
    try {
      // 测试获取所有勋章（应该包含潜在勋章）
      const allBadgesResponse = await axios.get('http://localhost:3000/api/badges', {
        headers: { Authorization: `Bearer ${authToken}` }
      });
      
      console.log(`📊 /api/badges 返回数量: ${allBadgesResponse.data.badges.length}`);
      
      if (allBadgesResponse.data.badges.length > 0) {
        console.log('\n前5个勋章详情:');
        allBadgesResponse.data.badges.slice(0, 5).forEach((badge, index) => {
          console.log(`勋章 ${index + 1}:`);
          console.log(`  - ID: ${badge.id}`);
          console.log(`  - 名称: ${badge.badge_name}`);
          console.log(`  - 状态: ${badge.status}`);
          console.log(`  - 用户ID: ${badge.user_id}`);
          console.log(`  - 活动ID: ${badge.activity_id}`);
          console.log(`  - 是否已拥有: ${badge.isOwned ? '✅' : '❌'}`);
          console.log(`  - 是否潜在勋章: ${typeof badge.id === 'string' && badge.id.startsWith('potential_') ? '✅' : '❌'}`);
          console.log('');
        });
      }
      
      // 测试获取用户拥有的勋章
      const ownedBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
        headers: { Authorization: `Bearer ${authToken}` }
      });
      
      console.log(`🎖️ /api/badges?filter=owned 返回数量: ${ownedBadgesResponse.data.badges.length}`);
      
    } catch (apiError) {
      console.log(`❌ API调用失败: ${apiError.message}`);
      if (apiError.response) {
        console.log(`   状态码: ${apiError.response.status}`);
        console.log(`   错误详情: ${JSON.stringify(apiError.response.data, null, 2)}`);
      }
    }
    
    // 3. 对比活动数量
    const activitiesResponse = await axios.get('http://localhost:3000/api/activities');
    console.log(`\n📅 系统活动总数: ${activitiesResponse.data.activities.length}`);
    
    // 4. 前端页面测试
    console.log('\n🌐 前端页面访问测试...');
    
    try {
      const frontendResponse = await axios.get('http://localhost:3000/app.html');
      console.log('✅ 前端页面可访问');
    } catch (frontendError) {
      console.log(`❌ 前端页面访问失败: ${frontendError.message}`);
    }
    
    console.log('\n📋 测试总结:');
    console.log('如果勋章数量现在等于活动数量，说明API修改成功');
    console.log('如果前端可以区分已获得/未获得勋章，说明显示逻辑正确');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 